package com.xingkeduo.business.user.service;

import com.xingkeduo.dao.database.DatabaseAccessObject;
import com.xingkeduo.pojo.query.CraftsmanFileQO;
import com.xingkeduo.pojo.table.CraftsmanFileTO;
import com.xingkeduo.util.pojo.enums.YesNoEnum;
import com.xingkeduo.util.pojo.exception.DataException;
import com.xingkeduo.util.tool.CommonUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Iterator;
import java.util.List;

/**
 * @Description:
 * @Author:gaozifeng
 * @Date 2019/2/18 18:29
 **/
@Service
public class CraftsmanFileService {
    private static final String NAMESPACE = CommonUtil.formatNamespace(CraftsmanFileTO.class);
    @Resource(name = "mysqlUserMasterDao")
    private DatabaseAccessObject<CraftsmanFileTO, CraftsmanFileQO> dao;


    /**
     * 发型师附件列表
     *
     * @return : List<CraftsmanFileTO>
     * @author : gaozifeng
     * @date : 2019年2月26日20:29:33
     */
    public List<CraftsmanFileTO> selectForList(CraftsmanFileQO craftsmanFileQO) throws Throwable {
        craftsmanFileQO.setIsDelete(YesNoEnum.NO);
        return dao.select(NAMESPACE, craftsmanFileQO);
    }


    @Transactional
    public int updateList(List<CraftsmanFileTO> fileTOS) throws Throwable {
        int count = 0;
        for (Iterator<CraftsmanFileTO> iterator = fileTOS.iterator(); iterator.hasNext(); ) {
            count += dao.update(NAMESPACE, iterator.next());
        }
        return count;
    }

    @Transactional
    public void update(CraftsmanFileTO fileTO) throws Throwable {
        if (dao.update(NAMESPACE, fileTO) < 1) {
            throw new DataException(DataException.Type.FAIL, "文件保存失败");
        }
    }

    @Transactional
    public void insertList(List<CraftsmanFileTO> fileTOS) throws Throwable {
        if (dao.insert(NAMESPACE, fileTOS) < fileTOS.size()) {
            throw new DataException(DataException.Type.FAIL, "文件保存失败");
        }
    }

    @Transactional
    public void insert(CraftsmanFileTO fileTOS) throws Throwable {
        if (dao.insert(NAMESPACE, fileTOS) < 1) {
            throw new DataException(DataException.Type.FAIL, "文件保存失败");
        }
    }


}
